package edu.ciee.app.jichuxinxi.action;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionContext;

import edu.ciee.app.action.util.JdbcUtil;
import edu.ciee.app.action.util.SessionUtil;

public class BuyerAction {

	/*
	 * ID int auto_increment, buyer_name varchar(50),
	 */
	private int id;
	private String buyer_name;
	private String yingye_code;
	private List resultList;
	
	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public List getResultList() {
		return resultList;
	}

	public void setResultList(List resultList) {
		this.resultList = resultList;
	}
	
	public String getYingye_code() {
		return yingye_code;
	}

	public void setYingye_code(String yingye_code) {
		this.yingye_code = yingye_code;
	}

	private String tip;

	public String getTip() {
		return tip;
	}

	public void setTip(String tip) {
		this.tip = tip;
	}

	public String getBuyer_name() {
		return buyer_name;
	}

	public void setBuyer_name(String buyer_name) {
		this.buyer_name = buyer_name;
	}

	public String insert() throws Exception {

		// System.out.println("this.getProduct_type()="+this.getProduct_type());

		String sql = "insert into buyer_table(buyer_name,yingye_code) values("
				+ "\'" + this.getBuyer_name() + "\',\'"
				+ this.getYingye_code() + "\');";

		if (JdbcUtil.updateResult(sql) == 0) {
			this.setTip("\n" + this.getTip()
					+ "No change was made, probably duplicate names.");
			return "error";
		} else {
			ActionContext.getContext().getSession().remove("buyermap");
			SessionUtil.updateSession("buyer_table", "buyer_name", "buyermap");

			this.setTip("Success."+this.getBuyer_name()+" added.");
			return "success";
		}
	}
	
	public String search() {

		String query = "where 1=1";

		if (this.getId()!=0) {
			query = query + " and ID like '%" + id + "%'";// this.getID()
		}
		if (this.getBuyer_name()!= null) {
			query = query + " and buyer_name like '%" + buyer_name + "%'"; // this.getName()
		}
		if (this.getYingye_code()!= null) {
			query = query + " and yingye_code like '%" + yingye_code + "%'"; // this.getName()
		}
		String sql = "select * from buyer_table " + query;
		System.out.println(sql);
		try {
			resultList = JdbcUtil.searchResult(sql);
			return "success";
		} catch (Exception e) {
			return "error";
		}
	}
	
	public String delete() {

		HttpServletRequest request = ServletActionContext.getRequest();
		ActionContext ctx = ActionContext.getContext();
		Map m = ctx.getParameters();
		String id = request.getParameter("id");
		System.out.println(id);
		String sql = "delete from buyer_table where ID=" + id + ";";
		System.out.println(sql);
		try {
			JdbcUtil.updateResult(sql);
			ActionContext.getContext().getSession().remove("buyermap");
			SessionUtil.updateSession("buyer_table", "buyer_name", "buyermap");
			return "refresh";
		} catch (Exception e) {
			this.setTip("error" + e.toString());
			return "error";
		}

	}
	
	public String forEdit(){
//		HttpServletRequest request = ServletActionContext.getRequest(); 
//		ActionContext ctx = ActionContext.getContext();		
//		Map m = ctx.getParameters();		
//		String name=request.getParameter("name");
//		int id=Integer.parseInt(request.getParameter("id"));
		System.out.println(id);
		System.out.println(buyer_name);
		System.out.println(yingye_code);
//		this.setName(name);
//		this.setId(id);
		
		return "success";
	}
	
	public String edit(){
		
		String sql = "update buyer_table set buyer_name = \'" + this.getBuyer_name()
		+ "\',yingye_code=\'" + this.getYingye_code()
		+ "\' where ID=" + this.getId() + ";";
		System.out.println(sql);
		try{
			JdbcUtil.updateResult(sql);
			ActionContext.getContext().getSession().remove("buyermap");
			SessionUtil.updateSession("buyer_table", "buyer_name", "buyermap");
			return "refresh";
		}catch(Exception e){
			return "error";
		}
	}

}
